#include <QDir>
#include <QDebug>
#include <QFileDialog>

#include "settings.h"

#include "symbiansettingsgui.h"
#include "ui_symbiansettingsgui.h"

SymbianSettingsGui::SymbianSettingsGui(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SymbianSettingsGui)
{
    ui->setupUi(this);

    // Read values
    ui->uid3Value->setText(Settings::get(Settings::UID3).toString());
    ui->qtenvValue->setText(Settings::get(Settings::QtEnvBatPath).toString()); // Note: check for Qt before mobility path! Mobility tries to guess based on Qt path.
    ui->logFileValue->setText(Settings::get(Settings::LogFilePath).toString());
    ui->layoutValue->setCurrentIndex(Settings::get(Settings::WidgetLayout).toInt());
    ui->checkWidgetPannable->setChecked(Settings::get(Settings::PanningEnabled).toBool());
    ui->checkTextSelectable->setChecked(Settings::get(Settings::TextSelectionEnabled).toBool());
    if (Settings::get(Settings::FullS60SDK).toBool())
    {
            ui->radioSDKS60->setChecked(true);
    }
    else
    {
            ui->radioSDKQt->setChecked(true);
    }

    // Connect browse buttons
    connect(ui->buttonBrowseBat, SIGNAL(clicked()), this, SLOT(browseForBat()));
    connect(ui->buttonBrowseLog, SIGNAL(clicked()), this, SLOT(browseForLog()));
}

SymbianSettingsGui::~SymbianSettingsGui()
{
    delete ui;
}

void SymbianSettingsGui::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void SymbianSettingsGui::saveSettings() const
{
    Settings::set(Settings::UID3, ui->uid3Value->text().trimmed());
    Settings::set(Settings::QtEnvBatPath, ui->qtenvValue->text().trimmed());
    Settings::set(Settings::LogFilePath, ui->logFileValue->text().trimmed());
    Settings::set(Settings::WidgetLayout, ui->layoutValue->currentIndex());
    Settings::set(Settings::PanningEnabled, ui->checkWidgetPannable->isChecked());
    Settings::set(Settings::TextSelectionEnabled, ui->checkTextSelectable->isChecked());
    Settings::set(Settings::FullS60SDK, ui->radioSDKS60->isChecked());
}


void SymbianSettingsGui::browseForBat()
{
    QFileInfo file = getFileInfo(Settings::get(Settings::QtEnvBatPath).toString());
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select qtenv.bat"), file.absolutePath(), tr("Batch files") + " (*.bat);;" + tr("All Files (*)"));
    if (!fileName.isEmpty())
    {
            fileName = QDir::toNativeSeparators(fileName);
            ui->qtenvValue->setText(fileName);
    }
}


void SymbianSettingsGui::browseForLog()
{
    // Existing
    QFileInfo file = getFileInfo(Settings::get(Settings::LogFilePath).toString());
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select application log file"), file.absolutePath(), tr("Log files") + " (*.log);;" + tr("All Files (*)"));
    if (!fileName.isEmpty())
    {
            fileName = QDir::toNativeSeparators(fileName);
            ui->logFileValue->setText(fileName);
    }
}


QFileInfo SymbianSettingsGui::getFileInfo(QString fileName)
{
    QFileInfo file(fileName);
    if (!QFile::exists(file.absolutePath()))
    {
            file = QFileInfo(QDir::root().absolutePath());
    }
    return file;
}
